package com.code.wanmi.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.code.wanmi.entity.wm_user.WmSysMenu;

import com.code.wanmi.entity.wm_user.WmSysUser;
import com.code.wanmi.util.DataSource;
import org.apache.ibatis.annotations.*;

import org.apache.ibatis.jdbc.SQL;

/**
 * <p>
 * 菜单权限表 Mapper 接口
 * </p>
 *
 * @author 谢晓峰
 * @since 2019-12-05
 */
@Mapper
public interface WmSysMenuMapper extends BaseMapper<WmSysMenu> {

    //更具菜单权限名查询菜单权限信息
    @DataSource("wmuser")
    @Select("SELECT\n" +
            "wm_sys_menu.menu_type,\n" +
            "wm_sys_menu.url,\n" +
            "wm_sys_menu.order_num,\n" +
            "wm_sys_menu.parent_code,\n" +
            "wm_sys_menu.static_code,\n" +
            "wm_sys_menu.menu_name,\n" +
            "wm_sys_menu.menu_id\n" +
            "FROM\n" +
            "wm_user.wm_sys_menu\n" +
            "where menu_name=#{menuName}")
    @Results({
            @Result(column = "menu_type", property = "userId"),
            @Result(column = "order_num", property = "userName"),
            @Result(column = "static_code", property = "userType"),
            @Result(column = "menu_name", property = "loginIp"),
            @Result(column = "menu_id", property = "loginDate"),
            @Result(column = "parent_code", property = "parent_code")
    })
    WmSysMenu findMenuByName(String menuName);

    @DataSource("wmuser")
    @Delete({
            "DELETE FROM wm_sys_role_menu WHERE menu_id=#{menu_id}"
    })
    Integer deleteRoleMenuByMenuId(@Param("menu_id")long menu_id);

    @DataSource("wmuser")
    @UpdateProvider(type = MenuSqlBuilder.class, method = "updateMenu")
    Integer updateMenu(WmSysMenu wmSysMenu);
    class MenuSqlBuilder {
        // If not use @Param, you should be define same arguments with mapper method
        public static String updateMenu(final WmSysMenu wmSysMenu) {
            return new SQL() {{
                UPDATE("wm_sys_menu");
                if (wmSysMenu.getParentCode() != null)
                    SET("parent_code = #{parentCode}");
                if (wmSysMenu.getMenuName() != null)
                    SET("menu_name = #{menuName}");
                if (wmSysMenu.getOrderNum() != null)
                    SET("order_num = #{orderNum}");
                WHERE("menu_id = #{menuId}");
            }}.toString();
        }


        // If use @Param, you can define only arguments to be used
//        public static String buildGetUsersByName(@Param("orderByColumn") final String orderByColumn) {
//            return new SQL(){{
//                SELECT("*");
//                FROM("users");
//                WHERE("name like #{name} || '%'");
//                ORDER_BY(orderByColumn);
//            }}.toString();
//        }
    }

}
